	//
	//  HomeViewController.m
	//  GaoDisplayManagerDemo
	//
	//  Created by Gikki Ares on 2020/8/5.
	//

#import "JsListViewController.h"
//这里用相对路径能找到头文件,但是有问题吧...
#import "../../01_Log/JsLogManager/JsLogManager/JsLogManager/JsLogManager.h"
#import "JsDisplayManager.h"

NSString * const JsRouterDicKeyDesc = @"desc";
NSString * const JsRouterDicKeySub = @"sub";
NSString * const JsRouterDicKeyTarget = @"target";
NSString * const JsRouterDicKeyTitle = @"title";

static NSString * TAG = @"JsListViewController";


@interface KeyInfo : NSObject

@property(nonatomic,strong)NSString * title;
@property(nonatomic,assign)uint8_t index;

-(instancetype)initWithTitle:(NSString *)title index:(int)index;

@end

@implementation KeyInfo

-(instancetype)initWithTitle:(NSString *)title index:(int)index {
	if(self = [super init]) {
		_title = title;
		_index = index;
	}
	return self;
}

@end

@interface JsListViewController ()<
UITableViewDelegate,
UITableViewDataSource
>
{
	UITableView *mtv;
	//记录key的路径.
	NSMutableArray <KeyInfo *>* mArr_routerPath;
	
}


@end

@implementation JsListViewController

#pragma mark 1 Life Circle

- (void)viewDidLoad {
	[super viewDidLoad];
		// Do any additional setup after loading the view from its nib.
		//此刻view的大小是xib的大小;如果是从xib加载;
	mtv = [[UITableView alloc] initWithFrame:self.view.bounds];
	mtv.backgroundColor = [UIColor whiteColor];
	[self.view addSubview:mtv];
	mtv.delegate = self;
	mtv.dataSource = self;
	[mtv registerClass:[JsTvcLll class] forCellReuseIdentifier:@"cell"];
	jslogi_c(@"JsListViewController",nil,@"viewDidLoad");
	
	
	if(self.routerArray) {
		self.marr_model = [self currentModelArray];
		[self->mtv reloadData];
	}
	else if(self.marr_model) {
		[self->mtv reloadData];
	}
}

	//- (UIUserInterfaceStyle)overrideUserInterfaceStyle {
	//	return UIUserInterfaceStyleLight;
	//}

#pragma mark 2 Interface
- (void)setMarr_model:(NSMutableArray<JsListViewControllerModel *> *)marr_model {
	_marr_model = marr_model;
	[mtv reloadData];
}

- (void)setDelegate:(id<JsListViewControllerDelegate>)delegate {
	
	_delegate = delegate;
	
		//习惯上在下一个周期内调用代理的方法;
	dispatch_async(dispatch_get_main_queue(), ^{
		if(!self.marr_model && !self.routerArray) {
			if(self.delegate) {
				if( [self.delegate respondsToSelector:@selector(jsListViewControllerSetDatasorce:)]) {
					self.marr_model = [[self.delegate jsListViewControllerSetDatasorce:self] mutableCopy];
					[self->mtv reloadData];
				}
				else if([self.delegate respondsToSelector:@selector(jsListViewControllerSetRouterArray:)]) {
					self.routerArray = [[self.delegate jsListViewControllerSetRouterArray:self] copy];
					mArr_routerPath = [NSMutableArray array];
					self.marr_model = [self currentModelArray];
					[self->mtv reloadData];
				}
			}
		}
	});
}

/**
 代码布局,必定要考虑view大小变化的场景.
 1,横竖屏的尺寸切换
 */
- (void)viewDidLayoutSubviews {
	self->mtv.frame = self.view.bounds;
}

#pragma mark 3 BasicFunction
	//当前router的数据源.
	//- (NSArray <JsListViewControllerModel *>*)currentRouterModelArray {
	//	NSMutableArray * marr_model = [NSMutableArray array];
	//	if(mDic_router.count) {
	//		NSDictionary * currentDic;
	//		if(mArr_routerPath.count) {
	//			NSString * keyPath = [mArr_routerPath componentsJoinedByString:@","];
	//			currentDic = [mDic_router valueForKeyPath:keyPath];
	//		}
	//		else {
	//			currentDic = mDic_router;
	//		}
	//		NSArray <NSString *>* array = mDic_router.allKeys;
	//		[array enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
	//			NSString * desc = mDic_router[obj][@"desc"];
	//			JsListViewControllerModel * model = [[JsListViewControllerModel alloc]initWithIndex:[NSString stringWithFormat:@"%02lu",idx] title:obj desc:desc];
	//			[marr_model addObject:model];
	//		}];
	//	}
	//	return marr_model;
	//}

//当前路径的dic,代表当前node的dic.
/**
 
 */

/**
 当前路径下的nodelist.
 */
-(NSArray<NSDictionary *> *)currentNodeList {
	__block NSDictionary * nodeDic = nil;
	__block NSArray<NSDictionary *> * nodeList = self.routerArray;
	[mArr_routerPath enumerateObjectsUsingBlock:^(KeyInfo *  _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
			nodeDic =  nodeList[key.index];
			nodeList = nodeDic[JsRouterDicKeySub];
	}];
	return nodeList;
}

/**
 当前路径的node的dic.
 */
- (NSDictionary *)currentNodeDic {
	__block NSDictionary * nodeDic = nil;
		//nodeList包含当前要显示的数组.
	__block NSArray<NSDictionary *> * nodeList = self.routerArray;
	if(self.routerArray) {
		[mArr_routerPath enumerateObjectsUsingBlock:^(KeyInfo *  _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
			nodeDic =  nodeList[key.index];
			nodeList = nodeDic[JsRouterDicKeySub];
		}];
	}
	return nodeDic;
}

/**
 1,当前nodeDic有配置sub		返回解析的数组.
 2,当前nodeDic没有配置sub		返回nil.
 */
- (NSMutableArray <JsListViewControllerModel *>*)currentModelArray {
	NSMutableArray * marr_model = [NSMutableArray array];
	NSArray <NSDictionary *> * currentNodeList = [self currentNodeList];
	if(currentNodeList){
		[currentNodeList enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
			NSString * title = dic[JsRouterDicKeyTitle];
			NSString * desc = dic[JsRouterDicKeyDesc];
			NSString * target = dic[JsRouterDicKeyTarget];
			JsListViewControllerModel * model = [[JsListViewControllerModel alloc]initWithIndex:[NSString stringWithFormat:@"%02lu",idx] title:title desc:desc target:target];
			[marr_model addObject:model];
		}];
		return marr_model;
	}
	else {
		return nil;
	}
}


#pragma mark 4 Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return _marr_model.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	JsTvcLll * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
	JsListViewControllerModel * model = _marr_model[indexPath.row];
	[cell displayModel:model];
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	JsListViewControllerModel * model = _marr_model[indexPath.row];
	if(self.routerArray){
		KeyInfo * keyInfo = [[KeyInfo alloc]initWithTitle:model.title index:indexPath.row];
		[mArr_routerPath addObject:keyInfo];
		NSDictionary * currentNodeDic = [self currentNodeDic];
		NSString * className = currentNodeDic[JsRouterDicKeyTarget];
		if(className.length) {
			[self gotoTargetViewController:className];
		}
		else  {
			NSArray * subModelArray = [self currentModelArray];
			if(subModelArray){
				[self.marr_model removeAllObjects];
				[self. marr_model addObjectsFromArray:subModelArray];
				[mtv reloadData];
			}
			else {
				[self.view displayMessage:@"No valid action key!"];
			}
		}
	}
	else {
		[self gotoTargetViewController:model.title];
		if(_delegate && [_delegate respondsToSelector:@selector(jsListViewController:clickAtIndex:)]) {
			[_delegate jsListViewController:self clickAtIndex:(int)indexPath.row];
		}
	}

}

- (void)gotoTargetViewController:(NSString *)className {
	Class class_navigationTarget = NSClassFromString(className);
	if(class_navigationTarget) {
		NSObject * instance = [class_navigationTarget new];
			//判断实例是否属于某个类.
		BOOL isViewController = [instance isKindOfClass:UIViewController.class];
			//判断类
			//		BOOL isViewController = [instance.class isKindOfClass:UIViewController.class];
		if(isViewController) {
			UIViewController * vc = (UIViewController *)instance;
			vc.title = className;
			switch(JsNavigationStylePush) {
				case JsNavigationStylePush: {
					[self.navigationController pushViewController:vc animated:YES];
					break;
				}
				case JsNavigationStylePresentFullScreen:{
					vc.modalPresentationStyle = UIModalPresentationFullScreen;
					[self.navigationController presentViewController:vc animated:YES completion:nil];
					break;
				}
				default: {
					[self.navigationController pushViewController:vc animated:YES];
					break;
				}
			}
		}
	}
	else {
		jslogf_fc(TAG, @"class %@ not exist!",className);
	}
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	JsListViewControllerModel * model = _marr_model[indexPath.row];
	return [model cellHeight] ;
}

#pragma mark 5 Event




@end
